package com.qyhl.webtv.module_news.luckydraw;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.flyco.tablayout.SlidingTabLayout;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.cloud.webtv.module_news.R;
import com.qyhl.cloud.webtv.module_news.R2;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.MPermissionUtils;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.news.SectionBean;
import com.qyhl.webtv.commonlib.utils.MShareBoard;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionLogUtils;
import com.qyhl.webtv.commonlib.utils.config.HomeThemeColorUtil;
import com.qyhl.webtv.module_news.luckydraw.fragment.LuckDrawFragment;
import com.qyhl.webtv.module_news.news.newslist.NewListFragment;
import com.umeng.socialize.UMShareAPI;
import com.weavey.loading.lib.LoadingLayout;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import es.dmoral.toasty.Toasty;

/**
 * Created by Administrator on 2018/6/8.
 * 开票有喜
 */

@Route(path = ARouterPathConstant.LUCK_DRAW)
public class LuckDrawActivity extends BaseActivity implements LuckDrawContract.LuckDrawView {
    @BindView(R2.id.back)
    ImageView back;
    @BindView(R2.id.share_btn)
    ImageView shareBtn;
    @BindView(R2.id.title)
    TextView mTitle;
    @BindView(R2.id.tabLayout)
    SlidingTabLayout mTabLayout;
    @BindView(R2.id.viewPager)
    ViewPager mViewPager;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;

    //标题
    @Autowired(name = "title")
    String title;
    @Autowired(name = "id")
    String catalogId;

    private LuckDrawPresenter mPresenter;
    private List<SectionBean> mList;
    private List<String> titleList;
    private StringBuilder sections;

    //PageAdapter
    private LuckDrawPageAdapter mViewPageadapter;
    // Frgaments
    private List<Fragment> mFragmentList;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getContentView() {

        return R.layout.news_activity_luck_draw;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {

        mPresenter = new LuckDrawPresenter(this);
        initView();

    }

    @Override
    protected void setListener() {
        setListeners();
        mPresenter.getSections(catalogId, "1");
    }

    /**
     * 初始化
     */
    private void initView() {
        loadMask.setStatus(LoadingLayout.Loading);
        mList = new ArrayList<>();
        mTitle.setText(StringUtils.isEmpty(title) ? "开票有喜" : title);
    }

    /**
     * 设置监听
     */
    private void setListeners() {
        //重载
        loadMask.setOnReloadListener(view -> {
            loadMask.setReloadButtonText("加载中...");
            mPresenter.getSections(catalogId, "1");
        });

        //退出
        back.setOnClickListener(view -> finish());

        //分享
        shareBtn.setOnClickListener(view -> MPermissionUtils.requestPermissionsResult(LuckDrawActivity.this, 1, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, new MPermissionUtils.OnPermissionListener() {
            @Override
            public void onPermissionGranted() {
                if (StringUtils.isNotEmpty(catalogId)) {
                    MShareBoard shareBoard = new MShareBoard(LuckDrawActivity.this,catalogId,StringUtils.isEmpty(title) ? "开票有喜" : title,"","", CommonUtils.getInstance().getBannerId(),17);
                    shareBoard.showPopupWindow();
//                            ShareUtils.getInstance().goShare(catalogId, 17, "", StringUtils.isEmpty(title) ? "开票有喜" : title, "", LuckDrawActivity.this);
                } else {
                    Toasty.normal(LuckDrawActivity.this, "请加载完成后再分享！").show();
                }
            }

            @Override
            public void onPermissionDenied() {
                MPermissionUtils.showTipsDialog(LuckDrawActivity.this);
            }
        }));

    }

    @Override
    public void setDatas(List<SectionBean> beanList) {
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");
        mTabLayout.setVisibility(View.VISIBLE);
        mList = beanList;
        sections = new StringBuilder();
        mFragmentList = new ArrayList<>();
        titleList = new ArrayList<>();
        titleList.add("首页");

        for (int i = 0; i < mList.size(); i++) {
            mFragmentList.add(NewListFragment.newInstance(mList.get(i).getSectionId() + ""));
            titleList.add(mList.get(i).getSectionName());
            if (i == 0) {
                sections.append(mList.get(i).getSectionId() + "");
            } else {
                sections.append("," + mList.get(i).getSectionId());
            }
        }
        mFragmentList.add(0, LuckDrawFragment.newInstance(sections.toString(), catalogId, false,null));
        mViewPageadapter = new LuckDrawPageAdapter(getSupportFragmentManager(), mFragmentList);
        mViewPageadapter.setData(titleList);
        mViewPager.setAdapter(mViewPageadapter);
        try {
            Class<? extends SlidingTabLayout> aClass = mTabLayout.getClass();
            Field f = aClass.getDeclaredField("mTabPadding");
            f.setAccessible(true);
            f.set(mTabLayout, StringUtils.dip2px(this, 15));
        } catch (Exception e) {
            e.printStackTrace();
        }

        mTabLayout.setViewPager(mViewPager);
        mTabLayout.setTextSelectColor(HomeThemeColorUtil.getCheckColor());
        mTabLayout.setTextUnselectColor(HomeThemeColorUtil.getTabNormalColor());
        mTabLayout.getTitleView(0).getPaint().setFakeBoldText(true);
        mViewPageadapter.notifyDataSetChanged();

    }

    @Override
    public void setError(String msg) {
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setReloadButtonText("点击重试~");
        loadMask.setErrorText(msg);
    }

    @Override
    public void setNetError(String msg) {
        loadMask.setStatus(LoadingLayout.No_Network);
        loadMask.setReloadButtonText("点击重试~");
        loadMask.setNoNetworkText(msg);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void setEmpty() {
        mTabLayout.setVisibility(View.GONE);
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");
        mFragmentList = new ArrayList<>();
        titleList = new ArrayList<>();
        titleList.add("首页");
        mFragmentList.add(LuckDrawFragment.newInstance("", catalogId, false,null));

        mViewPageadapter = new LuckDrawPageAdapter(getSupportFragmentManager(), mFragmentList);
        mViewPageadapter.setData(titleList);
        mViewPager.setAdapter(mViewPageadapter);
        try {
            Class<? extends SlidingTabLayout> aClass = mTabLayout.getClass();
            Field f = aClass.getDeclaredField("mTabPadding");
            f.setAccessible(true);
            f.set(mTabLayout, StringUtils.dip2px(this, 15));
        } catch (Exception e) {
            e.printStackTrace();
        }

        mTabLayout.setViewPager(mViewPager);
        mTabLayout.setTextSelectColor(HomeThemeColorUtil.getCheckColor());
        mTabLayout.setTextUnselectColor(HomeThemeColorUtil.getTabNormalColor());
        mTabLayout.getTitleView(0).getPaint().setFakeBoldText(true);
        mViewPageadapter.notifyDataSetChanged();
    }


    @Override
    protected void onResume() {
        super.onResume();
        ActionLogUtils.getInstance().onResume(this, ActionConstant.LUCK_DRA_HOME);
    }

    @Override
    protected void onPause() {
        super.onPause();
        ActionLogUtils.getInstance().onPause(this, ActionConstant.LUCK_DRA_HOME);
    }
}
